package com.cxhc.app.api;


import com.cxhc.common.annotation.Anonymous;
import com.cxhc.common.core.domain.R;
import com.cxhc.common.core.domain.entity.SysMenu;
import com.cxhc.common.utils.SecurityUtils;
import com.cxhc.framework.web.service.SysPermissionService;
import com.cxhc.system.domain.SysNotice;
import com.cxhc.system.domain.app.HomeApp;
import com.cxhc.system.domain.app.SysUserEnterprise;
import com.cxhc.system.service.ISysMenuService;
import com.cxhc.system.service.ISysNoticeService;
import com.cxhc.system.service.app.IHomeAppService;
import com.cxhc.system.service.app.ISysUserEnterpriseService;
import com.cxhc.system.service.app.IWeatherService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.cxhc.common.utils.SecurityUtils.getUserId;


/**
 * 首页模块接口
 */
@RestController()
@RequestMapping("/api/home")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ApiHomeApp {

    @Resource
    private IHomeAppService homeAppService;
    @Resource
    private IWeatherService weatherService;
    @Resource
    private ISysNoticeService noticeService;
    @Resource
    private ISysUserEnterpriseService enterpriseService;
    @Autowired
    private ISysMenuService menuService;

    /**
     * 首页数据
     *
     * @return
     */
    @Anonymous
    @GetMapping("/data")
    private R<Map<String, Object>> data() {
        Map<String, Object> data = new HashMap<>();
        boolean isLogin = false;
        Long userId = SecurityUtils.loginUserId();
        List<HomeApp> appList = new ArrayList<>();
        SysUserEnterprise enterprise = null;
        if (userId != null) {
            isLogin = true;
            enterprise = enterpriseService.selectSysUserEnterpriseByUserId(userId);
            appList = menuService.selectMenuListForApp(new SysMenu(), userId);

        }
        // "weather", weatherService.getWeather("慈溪"),
        data.put("isLogin", isLogin);
        data.put("enterprise", enterprise);
        data.put("appList", appList);
        data.put("noticeList", noticeService.selectNoticeList(new SysNotice().setStatus("0")));
        return R.ok(data);
//        return R.ok(ImmutableMap.of(
//                "isLogin",isLogin,
//                "enterprise", enterprise,
//                "appList", appList,
//                "noticeList", noticeService.selectNoticeList(new SysNotice().setStatus("0"))));
    }

    /**
     * 首页菜单【Anonymous 代表不需要登录】
     *
     * @return
     */
    @Anonymous
    @GetMapping("/menu")
    private R<List<HomeApp>> menu() {
//        Long userId = SecurityUtils.loginUserId();
//        if (userId == null) {
//            return R.ok(new ArrayList<>());
//        }
        return R.ok(homeAppService.selectHomeAppList(new HomeApp()));
    }

}
